package fy.uap.operatorImpl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import fy.form.listen.dao.MyOrgManager;
import fy.form.listen.log.LogWriter;
import fy.form.listen.set.OA_UAPAccountContrast;
import fy.msg.MessageSender;
import fy.rest.AForm_Save_Operator_Rest;
import fy.uap.dao.AcceptanceDao;
import fy.util.MyUtil;

public class AcceptanceOperator extends AForm_Save_Operator_Rest {

    private static Log log = LogFactory.getLog(AcceptanceOperator.class);

    private AcceptanceReader ar = new AcceptanceReader();

    private String pinpai;

    // private static String[] corpArray = {"1025","1024", "1002", "1004",
    // "1005" };
    public String getFormName() {
        return "计算机及周边设备验收";
    }

    @Override
    protected void parseValueExport(String name, String value) {
        // insert into eam_asset_it_i (PK_ASSET_IT_I, PK_CORP, C_DATE,
        // OA_NUMBER, ASSETCODE, FY_CODE, ASSETNAME, ACCEPT_DATE, WARRANTY,
        // ASSET_MODEL, MANUFACTURER, SERIALNUMBER, SERVICE_NO, CONFIG,
        // MONITOR_SN, MOUSE_SN, KEYBOARD_SN, BATTERY_SN, POWERADAPTER_SN, MAC,
        // WIRELESS_MAC)
        // values ('主键', '公司名称', '日期', '单据编号', '固定资产编号', '管理编号', '固定资产名称',
        // '验收日期', '保修期', '规格/型号', '生产厂商/销售厂商', '序列号', '服务代码', '详细配置', '显示器SN',
        // '鼠标SN', '键盘SN', '电池SN', '电源适配器SN', 'MAC地址', '无线MAC地址');
        // '主键', '公司名称', '日期', '单据编号',
        // '固定资产编号', '管理编号', '固定资产名称',
        // '验收日期', '保修期', '规格/型号', '生产厂商/销售厂商',
        // '序列号', '服务代码', '详细配置', '显示器SN', '鼠标SN',
        // '键盘SN', '电池SN', '电源适配器SN', 'MAC地址', '无线MAC地址'
        if ("单位选择器".equals(name)) {
            ar.setPkCorp(OA_UAPAccountContrast.getUapAccountID(value));
        } else if ("品牌".equals(name)) {
            pinpai = value;
        } else if ("使用人".equals(name)) {
            ar.setUserCode(value);
        } else {
            ar.read(name, value);
        }
    }

    @Override
    protected boolean save() {
        String msg;

        AcceptanceDao dao = new AcceptanceDao();
        String[] content = ar.getValues();
        content[8] = pinpai + "   " + content[8];

        try {
            content[content.length - 1] = MyOrgManager.getInstance()
                    .getMemberByID(Long.valueOf(content[content.length - 1]))
                    .getCode();
            if (dao.insert(content)) {
                log.info("成功写入单据："
                        + super.formBean.getFlowExport().getFlowTitle());
                return true;
            } else {
                msg = "计算机及周边设备验收单数据写入UAP系统发生异常";
            }
        } catch (Exception e) {
            msg = "计算机及周边设备验收单获取使用人信息发生异常，数据无法写入UAP";
            log.error(msg, e);
            return false;
        }

        MessageSender.sendMsg(new long[]{
            super.formBean.getFlowExport().getFlowCreatePerson().getId(),
            super.formBean.getOperatorID()}, msg);
        LogWriter.writeLog(MyUtil.getRealPath(this.getClass()), msg);
        log.error(msg);
        return false;
    }

}

class AcceptanceReader {

    private static final String[] fieldNames = "日期,单据编号,固定资产编号,管理编号,固定资产名称,验收日期,保修期,规格型号,生产厂商销售厂商,序列号,服务代码,详细配置,显示器SN,鼠标SN,键盘SN,电池SN,电源适配器SN,MAC地址,无线MAC地址,合计金额,计算机名"
            .split(",");

    private String[] values;

    public AcceptanceReader() {
        values = new String[fieldNames.length + 2];
    }

    public void read(String name, String value) {
        for (int i = 0; i < fieldNames.length; i++) {
            if (name.equals(fieldNames[i])) {
                values[i + 1] = (name.contains("日期") ? value.substring(0, 10) : value);
            }
        }
    }

    public void setPkCorp(String pk_corp) {
        values[0] = pk_corp;
    }

    public void setUserCode(String code) {
        values[fieldNames.length + 1] = code;
    }

    public String[] getValues() {
        return values;
    }
}
